package com.comdo.newup_loan.module.withdrawals;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.BankWebViewActivity;
import com.comdo.newup_loan.R;
import com.comdo.newup_loan.base.BaseActivity;
import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.module.PresentRecord.PresentRecordActivity;
import com.comdo.newup_loan.util.PointLengthFilter;
import com.comdo.newup_loan.util.StringUtils;
import com.comdo.newup_loan.util.TitleMenuUtil;
import com.comdo.newup_loan.widget.MyEditText;
import com.umeng.analytics.MobclickAgent;


//提现，取钱界面
public class WithdrawalsActivity extends BaseActivity<WithdrawalsPersenter> implements WithdrawalsContact.View {

	// 提现人，银行卡号，银行卡名,账户可用余额
	private TextView mTvbankGuy, mTvbankCard, mTvbankName, mTvAccount,mRecordTv;
	// 提现金额，验证码
	private MyEditText mEtMoney;
	private Button  mSubmit;
	private String requestNo;
	private String amount;
	@Override
	protected int getContentViewLayoutID() {
		return R.layout.activity_mine_withdrawals;
	}

	@Override
	protected void init() {
		super.init();
	}

	@Override
	protected void initView(Bundle savedInstanceState) {
		initToolBar();
		mTvbankGuy = (TextView) findViewById(R.id.tv_bankguy);
		mTvbankCard = (TextView) findViewById(R.id.tv_bankcard);
		mTvbankName = (TextView) findViewById(R.id.tv_bankname);
		mTvAccount = (TextView) findViewById(R.id.tv_accountmoney1);
		mRecordTv=(TextView)findViewById(R.id.tv_record);
		mEtMoney = (MyEditText) findViewById(R.id.et_takemoney);
		mEtMoney.setFilters(new InputFilter[]{new PointLengthFilter()});
		if(Config.userInfo!=null){
			mTvbankGuy.setText(Config.userInfo.getName());
			mTvbankCard.setText(Config.userInfo.getBankAccount());
			mTvbankName.setText(Config.userInfo.getBankName());
		}

		mSubmit = (Button) findViewById(R.id.bt_submittixian);
		mSubmit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {

				if (StringUtils.isNull(mEtMoney.getText().toString())) {
					Toast.makeText(getApplicationContext(), "请输入提现金额", Toast.LENGTH_SHORT)
							.show();
					return;
				}

				if(StringUtils.isNull(Config.userInfo.getBankAccount())){
					if(StringUtils.isNull(Config.userInfo.getBankAccount())){

						Toast.makeText(getApplicationContext(), "你尚未绑定银行卡！", Toast.LENGTH_SHORT).show();
						return;
					}
				}
				submit();
			}
		});
		if (Config.userInfo != null)
			if(Double.parseDouble(Config.userInfo.getAvailable())<0){
				mTvAccount.setText("0元");
			}else{
				mTvAccount.setText(StringUtils.getMoneyString(Double.parseDouble(Config.userInfo.getAvailable()))
						+ "元");
			}

		mRecordTv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(WithdrawalsActivity.this,PresentRecordActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public WithdrawalsPersenter getmPresenter() {
		return new WithdrawalsPersenter();
	}

	@Override
	public void presenterSetView() {
		if (mPresenter != null)
			mPresenter.setView(this);
	}

	private void submit() {
		amount=mEtMoney.getText().toString();
		mPresenter.getCommission(Config.loginInfo.getUserId(),amount);
	}
	@Override
	public void getFail(String failMessage) {
		showToast(failMessage);
	}

	@Override
	public void getCommission(String commission, String requestNo) {
		mPresenter.getBankWeb(WithdrawalsActivity.this,requestNo,Double.parseDouble(amount),Double.parseDouble(commission));
	}

	@Override
	public void getBankWeb(String url) {
		if(!StringUtils.isNull(url)){
			toXwWeb(url);
		}else{
			showToast("page not found");
		}
	}


	private void initToolBar(){
		new TitleMenuUtil(WithdrawalsActivity.this,"提现",false);
	}
	private void toXwWeb(String url){
		finish();
		Intent intent=new Intent(WithdrawalsActivity.this, BankWebViewActivity.class);
		intent.putExtra("url",url);
		startActivity(intent);
	}

	@Override
	protected int setStatusBarColor() {
		return getResources().getColor(R.color.colorTheme);
	}

	@Override
	protected boolean translucentStatusBar() {
		return false;
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("提现");
		MobclickAgent.onResume(this);
	}
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("提现");
		MobclickAgent.onPause(this);
	}
}
